require(["config"], function() {
	require(["jquery", "ejs"], function($, ejs) {
		function Provider() {
			this.loadData();
		}

		Provider.ModelTemplate = `<tr>
				                <td class="prov_code"><%= prov_code %></td>
				                <td class=""><%= prov_name %></td>
				                <td><%= linkman %></td>
				                <td><%= telephone %></td>
				                <td><%= fax %></td>
				                <td><%= create_time %></td>
				                <td>
				                    <a ><img src="/img/read.png" class="view" alt="查看" title="查看"/></a>
				                    <a ><img src="/img/xiugai.png" class="btn-modify" alt="修改" title="修改"/></a>
				                    <a><img src="/img/schu.png"  class="removeProvider"  alt="删除" title="删除"/></a>
				                </td>
				            </tr>`;

		$.extend(Provider.prototype, {
			loadData() {
				const url = "/api/providers/find_by_page?page=1";
				$.getJSON(url, (data) => {
					if(data.res_code === 1) {
						let html = "";
						data.res_body.data.forEach((curr) => {
							html += ejs.render(Provider.ModelTemplate, curr);
						});
						$(".providerTable>tbody").html(html);
					}
				});
				//数据加载完毕后，执行监听事件
				this.addListener();
			},
			addListener() {
				//获取供应商名称进行查询
				$(".btn-search").on("click", this.searchHandler);
				$(".btn-save").on("click", this.addHandler);
				$("tbody").on("click", $.proxy(this.viewJumpHandler, this));
				$("tbody").on("click", $.proxy(this.modifyJumpHandler, this));
				$("tbody").on("click", $.proxy(this.delHandler, this));
			},
			//点击查询按钮，获取输入的数据，传到后台
			searchHandler() {
				const search = $(".search_provider").val();
				const url = "/api/providers/find_by_prov_name?prov_name=" + search;
				$.getJSON(url, (data) => {
					if(data.res_code === 1) {
						let html = "";
						data.res_body.data.forEach((curr) => {
							html += ejs.render(Provider.ModelTemplate, curr);
						});
						$(".providerTable>tbody").html(html);
					}
				});
			},
			addHandler() {
				//获取表单数据
				const data = $(".addProv-form").serialize();
				const url = "/api/providers/add";
				$.ajax({
					type: "post",
					url: url,
					data: data,
					dataType: "json",
					success(data) {
						if(data.res_code === 1) {
							alert("添加成功");
							location.href = "/html/providerList.html";
						}
					}
				});
			},
			viewJumpHandler(event) {
				const $src = $(event.target);
				const _tr = $src.parents("tr");
				if($src.attr("class") === "view") {
					const prov_code = _tr.find(".prov_code").text();
					location.href = "/html/providerView.html?prov_code=" + prov_code;
				}
			},
			modifyJumpHandler(event) {
				const $src = $(event.target);
				const _tr = $src.parents("tr");
				if($src.attr("class") === "btn-modify") {
					const prov_code = _tr.find(".prov_code").text();
					location.href = "/html/providerUpdate.html?prov_code=" + prov_code;
				}
			},
			delHandler(event) {
				const $src = $(event.target);
				const _tr = $src.parents("tr");
				if($src.attr("class") === "removeProvider") {
					const prov_code = _tr.find(".prov_code").text();
					const url = "/api/providers/find_by_prov_code";
					$.getJSON(url, {
						prov_code
					}, (data) => {
						console.log(data)
						if(data.res_code === 1) {
							const _id = data.res_body.data._id;
							console.log(_id)
							const url2 = "/api/providers/delete";
							$.post(url2, {
								_id
							}, (data) => {
								if(data.res_code === 1) {
									alert("删除成功");
									location.reload();
								}
							})
						}
					});
				}
			}
			/*分页查询*/
			//			loadByPage(page) {
			//
			//			}

		});
		new Provider();
	});
});